package com.ruibo.tax.utils;

import com.ruibo.tax.constants.TaxConstants;
import com.ruibo.tax.model.base.PackageHead;
import com.ruibo.tax.model.base.PackageNumber;
import com.ruibo.tax.model.base.XMLEncodeModel;
import com.ruibo.tax.model.dz.SSDZXX;
import com.ruibo.tax.model.dz.SSDZXXXMLEncodeModel;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.hibernate.internal.util.ConfigHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/14 18:00
 */
@Slf4j
@Component
public class XMLUtils {

    @Value("${baseFilePath}")
    private String baseFilePath;

    @Autowired
    private PackageNumber number;

    public static final String HJBH = "HJBH_";

    /**
     * 同步
     *
     * @param xml
     * @param xmlEncodeModel
     */
    public synchronized void creteFileSync(String xml, XMLEncodeModel xmlEncodeModel) {
        creteFile(xml, xmlEncodeModel);
    }

    /**
     * 异步
     *
     * @param xml
     * @param xmlEncodeModel
     */

    public void creteFile(String xml, XMLEncodeModel xmlEncodeModel) {
        log.info("开始处理文件");
        // 数据包类型
        String sjblx = xmlEncodeModel.getHeads().getSJBLX() + "_";
        // 数据编号
        String sjbbh = xmlEncodeModel.getHeads().getSJBBH() + ".xml";
        try {
            File file = new File(baseFilePath + xmlEncodeModel.getHeads().getSJBLX() + "\\");

            if (!file.exists()) {
                file.mkdirs();
            }
            File file2 = new File(baseFilePath + xmlEncodeModel.getHeads().getSJBLX() + "\\" + HJBH + sjblx + sjbbh);
            if (!file2.exists()) {
                file2.createNewFile();
            }
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file2), "GBK");
            osw.append(xml);
            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        log.info("处理文件结束");
    }

    /**
     * @param xmlEncodeModel 封装的生成的xml的实体
     * @param list           数据集合
     * @param type           数据类型
     * @return
     */
    public XMLEncodeModel data(XMLEncodeModel xmlEncodeModel, List list, String type) {
        StringBuilder stringBuilder = new StringBuilder();
        // 分装数据包头部
        PackageHead packageHead = new PackageHead();
        stringBuilder.append(TaxConstants.DWDM);
        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        String packageNumber = localDateTime.format(yyyyMMdd);

        stringBuilder.append(packageNumber);
        stringBuilder.append(number.getPackageID());
        //数据包编号   日期部分是由年月日组成
        packageHead.setSJBBH(stringBuilder.toString());
        //数据包类型
        packageHead.setSJBLX(type);
        // 单位代码
        packageHead.setDWDM(TaxConstants.DWDM);
        //单位名称
        packageHead.setDWMC(TaxConstants.DWMC);
        //记录数
        packageHead.setJLS(list.size() + "");

        //   生成日期精确到年月日时分秒
        DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String scrq = localDateTime.format(yyyyMMddHHmmss);
        // 生成日期
        packageHead.setSCRQ(scrq);
        xmlEncodeModel.setHeads(packageHead);
        xmlEncodeModel.setRoots(list);
        return xmlEncodeModel;
    }

    /**
     * @param xmlEncodeModel 封装的生成的xml的实体
     * @param ssdzxx         数据集合
     * @param type           数据类型
     * @return
     */
    public SSDZXXXMLEncodeModel data(SSDZXXXMLEncodeModel xmlEncodeModel, SSDZXX ssdzxx, String type) {
        StringBuilder stringBuilder = new StringBuilder();
        // 分装数据包头部
        PackageHead packageHead = new PackageHead();
        stringBuilder.append(TaxConstants.DWDM);
        LocalDateTime localDateTime = LocalDateTime.now();

        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        String packageNumber = localDateTime.format(yyyyMMdd);

        stringBuilder.append(packageNumber);
        stringBuilder.append(number.getPackageID());
        //数据包编号   日期部分是由年月日组成
        packageHead.setSJBBH(stringBuilder.toString());
        //数据包类型
        packageHead.setSJBLX(type);
        // 单位代码
        packageHead.setDWDM(TaxConstants.DWDM);
        //单位名称
        packageHead.setDWMC(TaxConstants.DWMC);
        //记录数
        packageHead.setJLS(1 + "");

        //   生成日期精确到年月日时分秒
        DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String scrq = localDateTime.format(yyyyMMddHHmmss);
        // 生成日期
        packageHead.setSCRQ(scrq);
        xmlEncodeModel.setHeads(packageHead);
        xmlEncodeModel.setRoots(ssdzxx);
        return xmlEncodeModel;
    }
}
